package com.yanqu.road.dao.impl.activity.peakstronghold;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakstronghold.UserPeakStrongholdWarData;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserPeakStrongholdWarDataDaoImpl extends TempDataObjectDao<UserPeakStrongholdWarData> {
    public static String getUserStrongholdBattleDataSql = "select * from t_u_peakstronghold_data where userId=? ";

    public Map<Integer, UserPeakStrongholdWarData> getStrongholdWarUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserPeakStrongholdWarData> dataMap = new ConcurrentHashMap<>(8);
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserStrongholdBattleDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserPeakStrongholdWarData data = parse(rs);
                dataMap.put(data.getActivityId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserStrongholdBattleDataSql = "update t_u_peakstronghold_data set donateExp=?,donateNumMap=?,silverAmount=?,silverRound=?,resetPatronsRound=?,totalDamage=?,isWorship=? " +
            "where userId=? and activityId=?";

    @Override
    protected boolean updateData(UserPeakStrongholdWarData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getDonateExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDonateNumMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getSilverAmount().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSilverRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getResetPatronsRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTotalDamage().toString()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isWorship()));

        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateUserStrongholdBattleDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserStrongholdBattleDataSql = "insert into t_u_peakstronghold_data" +
            "(userId,activityId,donateExp,donateNumMap,silverAmount,silverRound,resetPatronsRound,totalDamage,isWorship) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserPeakStrongholdWarData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDonateExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDonateNumMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getSilverAmount().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSilverRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getResetPatronsRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTotalDamage().toString()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isWorship()));
        if (getDb().executeNoneQuery(insertUserStrongholdBattleDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserPeakStrongholdWarData parseData(ResultSet rs) throws SQLException {
        UserPeakStrongholdWarData data = new UserPeakStrongholdWarData();
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setSilverAmount(new BigInteger(rs.getString("silverAmount")));
        data.setSilverRound(rs.getInt("silverRound"));
        data.setDonateExp(rs.getInt("donateExp"));
        data.setDonateNumMap(JSON.parseObject(rs.getString("donateNumMap"), new TypeReference<Map<Integer, Integer>>() {
        }));
        data.setResetPatronsRound(rs.getInt("resetPatronsRound"));
        data.setTotalDamage(new BigInteger(rs.getString("totalDamage")));
        data.setWorship(rs.getBoolean("isWorship"));
        return data;
    }
}
